#pragma once

#include "IXFixedBuffer.h"
#include "XBaseObjImpl.h"

template < class T >
class TXFixedBufferImpl : public CXBaseObjImpl<T>
{
public:
    TXFixedBufferImpl()
    {
        m_pData = NULL;
        m_dwSize = 0;
        m_dwOffset = 0;
    }

    virtual ~TXFixedBufferImpl()
    {
        ReleaseData();
    }

public:
    // IXFixedBuffer
    virtual HRESULT STDMETHODCALLTYPE   put_Data(IN LPCVOID pData, IN DWORD dwOffset, IN DWORD dwSize)
    {
        ReleaseData();

        m_pData = pData;
        m_dwSize = dwSize;
        m_dwOffset = dwOffset;

        return S_OK;
    }
    virtual HRESULT STDMETHODCALLTYPE   get_Data(OUT LPCVOID* ppData, OUT LPDWORD pdwSize)
    {
        *ppData = (LPBYTE)m_pData + m_dwOffset;
        *pdwSize = m_dwSize;
        return S_OK;
    }

public:
    // IBaseObj
    virtual void    STDMETHODCALLTYPE   OnFinalRelease()
    {
        CXBaseObjImpl<T>::OnFinalRelease();
        ReleaseData();
    }

protected:
    virtual void    ReleaseData()
    {
        m_pData = NULL;
        m_dwSize = 0;
        m_dwOffset = 0;
    }

protected:
    LPCVOID     m_pData;
    DWORD       m_dwOffset;
    DWORD       m_dwSize;
};


class CXFixedBufferImpl : public TXFixedBufferImpl<IXFixedBuffer>
{
    typedef CXBaseObjImpl<IXFixedBuffer> superclass;
    DEF_SKIN_CLASS_SINGLE_INTF(CXFixedBufferImpl,
        superclass,
        gs_strGuidXSkin,
        IXFixedBuffer,
        0);
};